package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IRecruitBizService;
import com.kk.xhr.admin.controller.request.RecruitAddRequest;
import com.kk.xhr.admin.controller.request.RecruitModifyRequest;
import com.kk.xhr.admin.controller.request.RecruitQueryRequest;
import com.kk.xhr.admin.controller.vo.RecruitVO;
import com.kk.xhr.common.pojo.RecruitStatusEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 招聘管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "招聘管理", tags = "招聘管理")
@RequestMapping("/recruit")
public class RecruitController {
    private final Executor adminControllerBizExecutor;
    private final IRecruitBizService recruitBizService;

    @ApiOperation(value = "分页查询招聘")
    @RequiresRoles(value = {"admin", "recruit"}, logical = Logical.OR)
    @GetMapping
    public CompletionStage<ApiResult<PageVO<RecruitVO>>> queryPage(@Valid RecruitQueryRequest request) {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(recruitBizService.queryPage(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "我的招聘计划")
    @GetMapping("mine")
    public CompletionStage<ApiResult<PageVO<RecruitVO>>> queryPageMine(@Valid RecruitQueryRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> ApiResult.success(recruitBizService.queryMine(userId, request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加招聘")
    @PostMapping
    @RequiresRoles(value = {"admin", "recruit"}, logical = Logical.OR)
    public CompletionStage<ApiResult<Void>> add(@RequestBody @Valid RecruitAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            recruitBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改招聘状态")
    @PutMapping("/status/{id:.+}")
    @RequiresRoles(value = {"admin", "recruit"}, logical = Logical.OR)
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, RecruitStatusEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            recruitBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改招聘")
    @RequiresRoles(value = {"admin", "recruit"}, logical = Logical.OR)
    @PutMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody RecruitModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            recruitBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
